package  
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	import xxl.robotlegs.bundles.mvcs.ModularBundle;
	import xxl.robotlegs.bundles.mvcs.MVCSBundle;
	import xxl.robotlegs.extensions.contextsignal.api.IContextSignal;
	import xxl.robotlegs.extensions.contextview.impl.ContextView;
	import xxl.robotlegs.extensions.directcommandmap.api.IDirectCommandMap;
	import xxl.robotlegs.extensions.mediatormap.api.IMediatorMap;
	import xxl.robotlegs.extensions.modularity.api.IModuleConnector;
	import xxl.robotlegs.framework.api.IContext;
	import xxl.robotlegs.framework.impl.Context;
	import xxl.signals.impl.Slot;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class Main7 extends Sprite 
	{
		private var txt:TextField;
		private var s:Sprite;
		private var count:int = 0;
		
		public function Main7() 
		{
			txt = new TextField();
			txt.multiline = txt.wordWrap = true;
			txt.autoSize = TextFieldAutoSize.LEFT;
			addChild(txt);
			stage.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		private function clickHandler(e:MouseEvent):void 
		{
			Slot.DEBUG_MODE = true;
			var time:int = getTimer();
			var str:String = "";
			if (s == null)
			{
				s = new Sprite();
				var c1:IContext = new Context().install(MVCSBundle).configure(new ContextView(s));
				addChild(s);
				
				var childS:Sprite = new Sprite();
				var c2:IContext = new Context().install(ModularBundle).configure(new ContextView(childS));
				s.addChild(childS);
				
				var m1:IModuleConnector = c1.injector.getInstance(IModuleConnector);
				m1.onChannel("aaa").relaySignal("show");
				//m1.onChannel("aaa").receiveSignal("show");
				var m2:IModuleConnector = c2.injector.getInstance(IModuleConnector);
				m2.onChannel("aaa").receiveSignal("show");
				
				//(c2.injector.getInstance(IContextSignal) as IContextSignal).addToMap("show", function():void { trace("show") } ).safelyExecute = true;
				var cs2:IContextSignal = c2.injector.getInstance(IContextSignal);
				cs2.addToMap("show", function(v1:String,v2:int,v3:int,v4:int):void { trace(arguments); } ).safelyExecute = true;
				(c1.injector.getInstance(IContextSignal) as IContextSignal).dispatch("show",1,2,3);
				(c1.injector.getInstance(IContextSignal) as IContextSignal).dispatch("show","100");
				
				var dcm2:IDirectCommandMap = c2.injector.getInstance(IDirectCommandMap);
				dcm2.map(Command1).withGuards(randomGuard).withGuards(trueGuard).execute();
				//var med1:IMediatorMap = c1.injector.getInstance(IMediatorMap);
				//med1.map(Test2).toMediator(Mm2);
				//
				//var med2:IMediatorMap = c2.injector.getInstance(IMediatorMap);
				//med2.map(Test2).toMediator(M3);
				
				var tmpTest2:Test2 = new Test2();
				tmpTest2.name = "child test2";
				s.addChild(tmpTest2);
				tmpTest2 = new Test2();
				tmpTest2.name = "root test2";
				s.addChild(tmpTest2);
				//如果child是独立MediatorMapExtension，可以保证mediator只在child中有效，但可能会导致子模块override父模块的定义
				//如果child没有独立MediatorMapExtension，child注册的mediator，也会在父模块中起效
				
				str = ++count + " add:" + (getTimer() - time) + "\n";
			}
			else
			{
				removeChild(s);
				s = null;
				str = count + " remove:" + (getTimer() - time) + "\n";
			}
			txt.appendText(str);
		}
		
		private function randomGuard():Boolean
		{
			return Math.random() > 0.5;
		}
		
		private function trueGuard():Boolean
		{
			return true;
		}
		
	}

}